// pages/dest/dest.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    datalist: null,
    provinceInfoDataList: null,
    currentTab: 0,
    clientHeight: 0,
    navHeight: 0,
    navHeightRaw: 0
  },

  switchNav: function (e) {
    var index = e.target.id
    this.setData({
      currentTab: index
    })
  },
  /* nav绑定页面 */
  change: function (e) {
    var index = e.detail.current;
    this.setData({
      currentTab: index
    })
  },

  getRpx: function (e) {
    var winWidth = wx.getSystemInfoSync().windowWidth;
    return e * 750 / winWidth;
  },

  getPx: function (e) {
    var winWidth = wx.getSystemInfoSync().windowWidth;
    return e / (750 / winWidth);
  },

  getCovidData: function () {
    var app = getApp()
    if (app.globalData.covidData == null) {
      wx.cloud.callFunction({
        name: 'getCovidInfo',
      }).then(res => {
        console.log("successGetCovid")
        var result = res.result.data
        app.globalData.covidData = result
      })
    }
  },

  getNavHeight: async function () {
    let getMenuButton = await wx.getMenuButtonBoundingClientRect();
    console.log(getMenuButton, 'getMenuButton')
    this.setData({
      navHeight: getMenuButton.bottom + this.getPx(90) + this.getPx(28),
      navHeightRaw: getMenuButton.bottom
    })
  },

  onLoad: function (options) {
    this.getNavHeight()
    this.getCovidData()
    /* 获取窗口高度 */
    var that = this
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          clientHeight: res.windowHeight,
        });
      }
    })
    /* 获取head高度 */

    if (this.data.provinceInfoDataList == null) {
      wx.showLoading({
        title: '加载中',
      })
    }
  },

  getNationInfo: async function () {
    var datalist = [{
      continent: "亚洲",
      nations: []
    }, {
      continent: "欧洲",
      nations: []
    }]
    var that = this
    const db = wx.cloud.database()
    await db.collection('nationInfo').where({
      continent: "亚洲"
    }).limit(5).get().then(res => {
      datalist[0].nations = res.data
    })
    await db.collection('nationInfo').where({
      continent: "欧洲"
    }).limit(5).get().then(res => {
      datalist[1].nations = res.data
    })
    console.log(datalist)
    wx.hideLoading()
    this.setData({
      datalist: datalist
    })
  },

  getProvinceInfo: function () {
    const db = wx.cloud.database()
    var that = this
    db.collection('provinceInfo').get().then(res => {
      console.log(res.data)
      that.setData({
        provinceInfoDataList: res.data
      })
      wx.hideLoading()
    })
  },

  getDbData: function () {
    this.getProvinceInfo()
    this.getNationInfo()
  },

  sorry: function () {
    wx.showToast({
      title: '敬请期待',
      icon: 'none', //如果要纯文本，不要icon，将值设为'none'
      duration: 2000
    })
  },

  showdistdetail: function (e) {
    console.log(e)
    var ismunicipality = e.currentTarget.dataset.ismunicipality
    var province = e.currentTarget.dataset.province
    if (ismunicipality) {
      wx.navigateTo({
        url: '../destdetail/destdetail?' + 'city=' + province
      })
    } else {
      wx.navigateTo({
        url: '../provinceDetail/provinceDetail?' + 'province=' + province
      })
    }

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getDbData()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    this.onLoad(); //重新加载onLoad()

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})